package com.turing.post.thirdinterface.impl;

import java.util.HashMap;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.thirdinterface.util.MapPMBSTradDetail;
import com.turing.post.thirdinterface.util.MapPMBSTradDetailDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "thirdinterface.impl.MapPMBSTradDetailDaoImpl")
@Remote
@Local
public class MapPMBSTradDetailDaoImpl implements MapPMBSTradDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public HashMap<String, String> getMapMerchant() {
		HashMap<String, String> map = new HashMap<String, String>();
		String sql = "select * from MAPMERCHANT order by merchantno";
		Query query = _em.createNativeQuery(sql);
		List<Object[]> obj = query.getResultList();
		if (obj != null && obj.size() > 0) {
			for (Object[] ob : obj) {
				map.put(ob[0].toString(), ob[1].toString());
			}
			return map;
		} else {
			return null;
		}
	}

//	@SuppressWarnings("unchecked")
//	@Override
//	public HashMap<String, String> getMapPIBSDelegate() {
//		HashMap<String, String> map = new HashMap<String, String>();
//		String sql = "select * from MapPIBSDelegate";
//		Query query = _em.createNativeQuery(sql);
//		List<Object[]> obj = query.getResultList();
//		if (obj != null && obj.size() > 0) {
//			for (Object[] ob : obj) {
//				map.put(ob[0].toString(), ob[1].toString());
//			}
//			return map;
//		} else {
//			return null;
//		}
//	}

	@Override
	public void insert(MapPMBSTradDetail m) throws PCPSApplicationException {
		GeneralDao.insert(_em, m);
	}

	@Override
	public MapPMBSTradDetail getMapPMBSTradDetailByPk(MapPMBSTradDetail pk) {
		return _em.find(MapPMBSTradDetail.class, pk);
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getNewMerchantNoByOldMerchantNo(String oldMerchantNo) {
		String sql = "select newmerchantno,merchantno from mapmerchant where merchantno='"
				+ oldMerchantNo + "'";
		Query query = _em.createNativeQuery(sql);
		List<Object[]> obj = query.getResultList();
		if (obj != null && obj.size() > 0) {
			return obj.get(0)[0].toString();
		} else {
			return null;
		}
	}

	@Override
	public void deleteMapPMBSTradDetail() {
		String sql = "delete from MapPMBSTradDetail";
		Query query = _em.createNativeQuery(sql);
		query.executeUpdate();
	}
}
